<?php
class UsersController extends AppController
{
    var $name = 'Users';
    var $helpers = array('Html', 'Form');
    var $paginate=array(
    'limit'=>'20',
    'order'=>array('User.name'=>'asc')
    );

    function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow(array('view', 'edit'));
        //$this->Auth->allow(array('*'));
    }

    function view()
    {
        $id=$this->Auth->user("id");
        $user = $this->User->findById($id);
        if(!$user) {
            $this->cakeError("error404");
            return;
        }
        $this->set(compact("user"));
    }

    function edit()
    {
        $id=$this->Auth->user("id");
        if(!$id && empty($this->data)) {
            $this->Session->setFlash("User is invalid!");
            $this->redirect(array('action'=>'view',$this->User->id));//ezzel ugrik vissza az előző oldalra
        }
        if ($this->data["User"]["password"] == "") {
            unset($this->data["User"]["password"]);
        }
        if (!empty($this->data)) {
            if($this->User->saveAll($this->data, array("validate"=>"first"))) {
                $this->Session->setFlash("The saving is ready!");
                $this->redirect(array('action'=>'view',$this->User->id));//ezzel ugrik vissza az előző oldalra
            }
            else {
                $this->Session->setFlash("The saving not ready, plase check the data");
            }

        }
        else {
            $this->User->recursive=2;
            $this->data = $this->User->read(null,$id);
            unset($this->data["User"]["password"]);
        }
    }

    function registration()
    {
        if (!empty($this->data)) {
            $this->User->create();
            $this->data['Group']['Group'][]='2';
            if($this->User->saveAll($this->data)) {
                $this->Session->setFlash("The saving is ready!");
                //$this->redirect('/');//ezzel ugrik vissza az előző oldalra
            }
        }
    }

    function admin_index()
    {
        //$this->User->recursive = -1;
        $this->set("user",$this->paginate());
    }

    function admin_add()
    {
        if (!empty($this->data)) {
            $this->User->create();
            if($this->User->saveAll($this->data)) {
                $this->Session->setFlash("The saving is ready!");
                $this->redirect(array("action"=>"index",$this->User->id));//ezzel ugrik vissza az előző oldalra
            }
        }
        $groups=$this->User->Group->find("list");
        $this->set(compact("groups"));
    }

    function admin_edit($id=null)
    {
        if(!$id && empty($this->data)) {
            $this->Session->setFlash("User is invalid!");
            $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
        }
        if (!empty($this->data)) {
            if ($this->data["User"]["password"] == "") {
                unset($this->data["User"]["password"]);
            }
            if($this->User->saveAll($this->data)) {
                $this->Session->setFlash("The saving is ready!");
                $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
                return;
            }
            else {
                $this->Session->setFlash("The saving not ready, plase check the data");
            }

        }
        if(empty($this->data)) {
            $this->User->recursive=2;
            $this->data = $this->User->read(null,$id);
            unset($this->data["User"]["password"]);

        }
        $groups = $this->User->Group->find("list");
        $this->set(compact("groups"));
    }

    function admin_delete($id=null)
    {
        if(!$id) {
            $this->Session->setFlash("User is invalid!");
            $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
        }
        if (!$this->User->del($id)) {
            $this->Session->setFlash("User not exists!");
        }
        else {
            $this->Session->setFlash("User deleted!");
        }
        $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
    }

    function admin_view($id=null)
    {
        if(!$id) {
            $this->Session->setFlash("User is invalid!");
            $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
        }
        $this->set('user',$this->User->read(null,$id));
    }

    function login()
    {
        if ($this->Auth->user()) {
           if($this->Authorize->check('administer data')){
               $this->redirect(
                   array(
                        'controller'=>'categories',
                        'admin'=>true
                    )
                );
           }else{
               $this->redirect($this->Auth->redirect());
           }
        }
    }

    function logout()
    {
        $this->redirect($this->Auth->logout());
    }

    function admin_login()
    {
        $this->redirect(array('action'=>'login', 'admin'=>false));
    }

    function admin_logout()
    {
        $this->redirect(array('action'=>'logout', 'admin'=>false));
    }
}
?>